package com.lanou3g.zjl.chating.home.setting;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;

import com.easemob.chat.EMChatManager;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseFragment;
import com.lanou3g.zjl.chating.home.message.ConversationRvAdapter;
import com.lanou3g.zjl.chating.home.message.MessageFragment;
import com.lanou3g.zjl.chating.home.widget.AddFriendsByQRCode;
import com.lanou3g.zjl.chating.user.login.LoginActivity;

import butterknife.Bind;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;


/**
 * Created by dllo on 15/11/17.
 */
public class SettingFragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private SharedPreferences sp;
    private SharedPreferences settingSp;
    private SharedPreferences.Editor settingEditor;
//    private ConversationRvAdapter adapter;

    @Bind(R.id.editLogin_Btn)
    Button editLogin_Btn;
    @Bind(R.id.acceptNews)
    EaseSwitchButton accept;
    @Bind(R.id.sound)
    EaseSwitchButton useSound;
    @Bind(R.id.shake)
    EaseSwitchButton useShake;
    @Bind(R.id.isUseLoudSpeaker)
    EaseSwitchButton useLoudSpeaker;
    @Bind(R.id.soundLayout)
    RelativeLayout soundLayout;
    @Bind(R.id.shakeLayout)
    RelativeLayout shakeLayout;

    //点击退出按钮退出应用程序
    @OnClick(R.id.editLogin_Btn)
    void editLogin() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("你确定要退出登录吗?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                EMChatManager.getInstance().logout();//此方法为同步方法
                sp = getActivity().getSharedPreferences("zy", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("name", null);
                editor.putString("psw", null);
                editor.commit();

                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
                onDestroy();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                return;
            }
        });
        builder.create().show();

    }

    //点击查看用户黑名单
    @OnClick(R.id.blackListLayout)
    void showBlackList() {
        Intent intent = new Intent(getActivity(), ContactBlackListActivity.class);
        startActivity(intent);
    }

    //查看个人资料
    @OnClick(R.id.profile)
    void toProfile() {
        Intent intent = new Intent(getActivity(), PersonCenterActivity.class);
        startActivity(intent);
    }


    @Override
    protected View getLayout(LayoutInflater inflater) {

        return inflater.inflate(R.layout.fragment_setting, null);
    }

    @Override
    protected void initData() {
        //初始化监听事件
        initOnclickListener();
        settingSp = getActivity().getSharedPreferences("SystemSetting", Context.MODE_PRIVATE);
        settingEditor = settingSp.edit();
        //初始化设置信息
        initSettingInfo();
        String username = EMChatManager.getInstance().getCurrentUser();
        editLogin_Btn.setText("退出登录" + username);
    }

    //初始化设置信息
    private void initSettingInfo() {
        boolean isAcceptNews = settingSp.getBoolean("isAcceptNews", true);
        boolean isUseSound = settingSp.getBoolean("isUseSound", true);
        boolean isUseShake = settingSp.getBoolean("isUseShake", true);
        boolean isUseLoudSpeaker = settingSp.getBoolean("isUseLoudSpeaker", true);
        if (isAcceptNews) {
            accept.openSwitch();
        } else {
            soundLayout.setVisibility(View.GONE);
            shakeLayout.setVisibility(View.GONE);
            accept.closeSwitch();

        }
        if (isUseSound) {
            useSound.openSwitch();
        } else {
            useSound.closeSwitch();
        }
        if (isUseShake) {
            useShake.openSwitch();
        } else {
            useShake.closeSwitch();
        }
        if (isUseLoudSpeaker) {
            useLoudSpeaker.openSwitch();
        } else {
            useLoudSpeaker.closeSwitch();
        }
    }

    //初始化监听事件
    private void initOnclickListener() {
        accept.setOnClickListener(this);
        useSound.setOnClickListener(this);
        useShake.setOnClickListener(this);
        useLoudSpeaker.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.acceptNews://是否接收新消息
                if (accept.isSwitchOpen()) {
                    soundLayout.setVisibility(View.GONE);
                    shakeLayout.setVisibility(View.GONE);
                    settingEditor.putBoolean("isAcceptNews", false);
                    settingEditor.commit();
                    accept.closeSwitch();
                } else {
                    soundLayout.setVisibility(View.VISIBLE);
                    shakeLayout.setVisibility(View.VISIBLE);
                    settingEditor.putBoolean("isAcceptNews", true);
                    settingEditor.commit();
                    accept.openSwitch();
                }
                String s = "isAcceptNews";
                EventBus.getDefault().post(s);
                break;
            case R.id.sound://新消息到来时是否发出提示音
                if (useSound.isSwitchOpen()) {
                    settingEditor.putBoolean("isUseSound", false);
                    settingEditor.commit();
                    useSound.closeSwitch();
                } else {
                    settingEditor.putBoolean("isUseSound", true);
                    settingEditor.commit();
                    useSound.openSwitch();
                }
                break;
            case R.id.shake://新消息到来时是否震动
                if (useShake.isSwitchOpen()) {
                    settingEditor.putBoolean("isUseShake", false);
                    settingEditor.commit();
                    useShake.closeSwitch();
                } else {
                    settingEditor.putBoolean("isUseShake", true);
                    settingEditor.commit();
                    useShake.openSwitch();
                }
                break;
            case R.id.isUseLoudSpeaker://接收语音消息时是否使用扬声器
                if (useLoudSpeaker.isSwitchOpen()) {
                    settingEditor.putBoolean("isUseLoudSpeaker", false);
                    settingEditor.commit();
                    useLoudSpeaker.closeSwitch();
                } else {
                    settingEditor.putBoolean("isUseLoudSpeaker", true);
                    settingEditor.commit();
                    useLoudSpeaker.openSwitch();
                }
                break;

        }
    }
}
